$(document).ready(function() {
    editHomePage.init();
});

var editHomePage = (function() {
    function editHomePage(){        
        $('.form-group').removeClass('has-error');
        $('.errorHandler').html('');
                
        var lstCateLocal = "";
        for(var i in $("#demo3").tagit("tags"))
            lstCateLocal += $("#demo3").tagit("tags")[i].value + ",";
        $('#demo3Out').html('<input type="hidden" id="postListCateLocal" name="postListCateLocal" value="' + lstCateLocal + '" />');
        
        var lstCateInter = "";
        for(var i in $("#demo8").tagit("tags"))
            lstCateInter += $("#demo8").tagit("tags")[i].value + ",";
        $('#demo8Out').html('<input type="hidden" id="postListCateInter" name="postListCateInter" value="' + lstCateInter + '" />');
                
        $('#appInput').ajaxSubmit({
            url: '/managehomepage/ajax-edit',
            type: 'post',
            dataType: 'json',
            timeout: 3000,
            success: function(resp) { 
                if (resp.err === 0) {                       
                   // $('#editHomePage').find('i').attr('class','fa fa-save');
                    //$('.errorHandler').html('<small><small><strong class="text-success">' + resp.msg + '</strong></small></small>').show(); 
                    //chuyen trang                    
                    window.location.href = '/managehomepage/edit';
                } 
                else {                    
                    $('.errorHandler').html('<small><small><strong class="text-danger">' + resp.msg + '</strong></small></small>').show();
                }
            }
        });                         
    }
    
    
    function initHanlder(){
        $('#editHomePage').click(function() {
            $(this).find('i.fa').attr('class', 'fa fa-spinner fa-spin');
            editHomePage();
            return false;
        });
             
        var hash = window.location.hash;
        if(hash.indexOf('#msg=') > -1){
            $('.errorHandler').html(hash.substring(5)).show();
            window.location.hash = '';
        }
    }
    return {
        init: function() {
            initHanlder();            
        }        
    };
})();